package cn.iocoder.yudao.module.blog.service.spacecomment;

import cn.iocoder.yudao.module.blog.dal.mysql.space.BlogSpaceMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.spacecomment.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacecomment.BlogSpaceCommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.spacecomment.BlogSpaceCommentConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.spacecomment.BlogSpaceCommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 动态评论 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogSpaceCommentServiceImpl implements BlogSpaceCommentService {

    @Resource
    private BlogSpaceCommentMapper spaceCommentMapper;

    @Resource
    private BlogSpaceMapper spaceMapper;

    @Override
    public Long createSpaceComment(BlogSpaceCommentCreateReqVO createReqVO) {
        // 插入
        BlogSpaceCommentDO spaceComment = BlogSpaceCommentConvert.INSTANCE.convert(createReqVO);
        spaceCommentMapper.insert(spaceComment);

        // 返回
        return spaceComment.getId();
    }

    @Override
    public void updateSpaceComment(BlogSpaceCommentUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpaceCommentExists(updateReqVO.getId());
        // 更新
        BlogSpaceCommentDO updateObj = BlogSpaceCommentConvert.INSTANCE.convert(updateReqVO);
        spaceCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpaceComment(Long id) {
        // 校验存在
        validateSpaceCommentExists(id);
        // 删除
        spaceCommentMapper.deleteById(id);
    }

    private void validateSpaceCommentExists(Long id) {
        if (spaceCommentMapper.selectById(id) == null) {
            throw exception(SPACE_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public BlogSpaceCommentDO getSpaceComment(Long id) {
        return spaceCommentMapper.selectById(id);
    }

    @Override
    public List<BlogSpaceCommentDO> getSpaceCommentList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return spaceCommentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogSpaceCommentDO> getSpaceCommentPage(BlogSpaceCommentPageReqVO pageReqVO) {
        return spaceCommentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogSpaceCommentDO> getSpaceCommentList(BlogSpaceCommentExportReqVO exportReqVO) {
        return spaceCommentMapper.selectList(exportReqVO);
    }

    @Override
    public Long getChildCount(Long commentId){
        return spaceCommentMapper.selectCount(BlogSpaceCommentDO::getMainCommentId, commentId);
    }

}
